package web

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"testPaper/common"
	"testPaper/model"
	"testPaper/response"
)

// UpdateTopic 更新题目
func UpdateTopic(c *gin.Context) {
	DB := common.GetDB()
	id := c.PostForm("id")
	subject := c.PostForm("subject")
	choose := c.PostForm("choose")
	img := c.PostForm("img")
	types := c.PostForm("type")
	rightKey := c.PostForm("rightKey")
	tagId := c.PostForm("tag_id")

	if len(subject) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "题目不能为空")
		return
	} else if len(choose) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "选择项不能为空值")
		return
	} else if len(rightKey) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "正确答案和题目说明不能为空")
		return
	} else if len(types) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "题目类型不能为空")
		return
	} else if tagId == "" {
		response.Reponse(c, http.StatusOK, 422, nil, "请选择标签")
		return
	} else if id == "" {
		response.Reponse(c, http.StatusOK, 422, nil, "题目ID不能为空")
		return
	}
	var oldTopic model.Topic
	newTopic := model.Topic{
		Subject:  subject,
		Choose:   choose,
		RightKey: rightKey,
		Type:     types,
		TagId:    tagId,
		Img:      img,
	}

	DB.Where("id = ?", id).First(&oldTopic).Update(&newTopic)
	response.Reponse(c, http.StatusOK, 200, nil, "更新成功")
	return
}
